H5x.component("activity.addform",[],function(opts){   
    this.container =  opts.container;
    this.opts = opts;
    this.use("render");
    this.use("dai");
    this.use("emitter");

    this.acitvitydai = this.dai.create("activity");

    //this.applistdai = this.dai.create("appsetting"); 
    this.init(); 

},{
    init:function(){
        var self = this;
        this.render("tp-activity-form",this.opts);

        $("#add-label-container").make("add-label",{
            propname:"clazz",
            value:this.opts.activity.clazz
        });
        $("#cover-container").make("cover-uploader",{
            propname:"coverurl",
            value:this.opts.activity.coverurl
        });
        $("#description-container").make("rich-editor",{
            propname:"content",
            value:this.opts.activity.content
        }); 

        $("input[name='enddate']").ECalendar({
            type:"date",
            stamp : false,
            skin:"#00a65a",
            offset:[0,2]
        });

        $("#activity-save-btn").click(function(){
            self.emitter.emit("form-submit");           //console.log($("#activity-add-form"));
            var data = $("#activity-add-form").getJson();

            if(data.id){
                var id = data.id;
                delete data.id;
                self.acitvitydai.update({id:id},data,function(rs){
                    $.tip("修改成功！");
                    //debugger;
                    if(rs.code>0){
                        //self.emitter.emit("activity-refresh");
                        self.opts.modal.modal("hide");
                        setTimeout(function(){
                            self.emitter.emit("activity-refresh");
                        },300);
                    }   
                });
            }else{
                self.acitvitydai.add(data,function(rs){
                    $.tip("编辑成功！");
                    if(rs.code>0){
                        //self.emitter.emit("activity-refresh");
                        self.opts.modal.modal("hide");
                        setTimeout(function(){
                            self.emitter.emit("activity-refresh");
                        },300);
                    }
                });
            }

            
        });
    }
});